package org.todd.healthreport.service.Impl;

import org.openqa.selenium.OutputType;
import org.openqa.selenium.Point;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.springframework.stereotype.Service;
import org.springframework.util.FileCopyUtils;
import org.todd.healthreport.service.KsonService;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;

@Service
public class KsonServiceImpl extends BaseChromeDriver implements KsonService {

    private static final String requestUrl = "http://125.222.107.68:8088/ksxt/ksonline/index.jsp";

    private static final String username = "81137332";

    private static final String passwords = "000000";

    ChromeDriver chrome = null;

    public void loginHandle() {
        chrome = super.getChromeDriver();
        chrome.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
        chrome.get(requestUrl);
        WebElement id = chrome.findElementById("id");
        id.sendKeys(username);
        WebElement password = chrome.findElementById("password");
        password.sendKeys(passwords);
        WebElement signIn = chrome.findElementById("signIn");
        signIn.click();
        WebElement elementByXPath = chrome.findElementByXPath("/html/body/div[3]/div[2]/ul[1]/form/li/a");
        elementByXPath.click();
        WebElement exam = chrome.findElementByXPath("/html/body/form/div[3]/div/div/div[3]/table/tbody/tr[1]/td[1]");
        exam.click();
        Set<String> windowHandles = chrome.getWindowHandles();
        List<String> it = new ArrayList<String>(windowHandles);
        chrome.switchTo().window(it.get(1));
        WebElement c_z_white = chrome.findElementByClassName("c_z_white");
        c_z_white.click();
        WebElement a = chrome.findElementByXPath("//*[@id=\"djstext0\"]/a");
        a.click();
        examHandle();

    }

    public void examHandle() {
        for (int i = 0; i < 1000; i++) {
        File screenshotAs = chrome.getScreenshotAs(OutputType.FILE);
        try {
            FileCopyUtils.copy(screenshotAs,new File("D:\\screenshoot\\test"+i+".png"));
        } catch (IOException e) {
            e.printStackTrace();
        }

            List<WebElement> button = chrome.findElementsByTagName("a");
            for (WebElement we:button) {
                String text = we.getAttribute("onclick");
                if (text==null) continue;
                System.out.printf(text);
                if ("clickcsh_h5();document.getElementById('ifm').contentWindow.nextti('0');".equals(text)){
                    we.click();
                    try {
                        Thread.sleep(2000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    break;
                }
            }
        }
    }
}
